<?php

namespace JsonLowCode\UI;

use JsonLowCode\Schema;
use JsonLowCode\Traits\Props\HasDisplayDirective;

/**
 * Collapse 组件
 * @method static Collapse make(mixed $children = null) Collapse组件
 */
class Collapse extends Schema
{
    use HasDisplayDirective;

    protected string $componentName = "NCollapse";

    public function __construct(mixed $children = null)
    {
        parent::__construct();
        $this->children($children);
    }

    /**
     * 是否只允许展开一个面板
     * @param bool|string $bool
     * @return $this
     */
    public function accordion(bool|string $bool = true): static
    {
        return $this->prop('accordion', $bool);
    }

    /**
     * 箭头位置
     * @param string $arrowPlacement 'left' | 'right'
     * @return $this
     */
    public function arrowPlacement(string $arrowPlacement = 'left'): static
    {
        return $this->prop('arrow-placement', $arrowPlacement);
    }

    /**
     * 非受控模式下展开的面板 name。accordion 模式时不为数组
     * @param string|int|array $defaultExpandedNames string | number | Array(string|number) | null
     * @return $this
     */
    public function defaultExpandedNames(string|int|array $defaultExpandedNames): static
    {
        return $this->prop('default-expanded-names', $defaultExpandedNames);
    }

    /**
     * 受控模式下展开的面板的 name，accordion 模式时不为数组
     * @param string|int|array $expandedNames string | number | Array(string|number) | null
     * @return $this
     */
    public function expandedNames(string|int|array $expandedNames): static
    {
        return $this->prop('expanded-names', $expandedNames);
    }

    /**
     * 展开内容改变时触发的回调函数
     * @param string $value (expandedNames: Array(string|number) | string | number | null) => void
     * @return $this
     */
    public function onUpdateExpandedNames(string $value = "(expandedNames)=>{}"): static
    {
        return $this->events('onUpdate:expanded-names', $value);
    }

    /**
     * 点击标题时触发的回调函数
     * @param string $value (data: { name: string | number, expanded: boolean, event: MouseEvent }) => void
     * @return $this
     */
    public function onItemHeaderClick(string $value = "(data)=>{}"): static
    {
        return $this->events('onItemHeaderClick', $value);
    }

    /**
     * 折叠面板的自定义图标 设置 slot arrow 内容
     * (props: { collapsed: boolean })
     * @param string|array|Schema $arrow 是否是js表达式
     * @return $this
     */
    public function slotArrow(string|array|Schema $arrow): static
    {
        return $this->slot('arrow', $arrow);
    }

    /**
     * 设置CollapseItem
     * @param string|null $title
     * @param mixed|null $children
     * @return CollapseItem
     */
    public function item(string $title = null,  mixed $children = null): CollapseItem
    {
        $item = new CollapseItem($title,$children);
        $this->children($item);
        return $item;
    }


}
